<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Needs extends Modules_Controller
{

	function __construct()
	{
		parent::__construct();
        $this->load->model('needs_model','mneeds');
        $this->load->model('claim_model','mclaim');
        $this->load->model('need_order_model','mneedorder');
        $this->load->model('balance_detail_model','mbalance');

    }

    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];
        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        // 条件必须
        $cid=$this->cid;
        $this->db->select('*,a.id as nid');
        $this->db->from('boc_needs as a');
        $this->db->join('boc_account as b','a.uid=b.id','left');
        $this->db->join('boc_category as c','a.cate_id=c.id','left');
        $arr = array();
        if($this->cid==13){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['intro']) and $_GET['intro']){
                $arr['like']=array('a.intro',$_GET['intro']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            if(isset($_GET['claim_status']) and $_GET['claim_status'] >= 0){
                $arr['claim_status']=$_GET['claim_status'];
            }
            if(isset($_GET['arbitrate']) and $_GET['arbitrate']){
                $arr['arbitrate']=$_GET['arbitrate'];
            }
            $where1 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>1,'a.is_deleted'=>0);
            $where = array_merge($arr,$where1);
        }elseif($this->cid==14){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            $where2 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>2,'a.is_deleted'=>0);
            $where = array_merge($arr,$where2);
        }elseif($this->cid==15){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            $where3 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>3,'a.is_deleted'=>0);
            $where = array_merge($arr,$where3);
        }
        $this->db->where($where);
        if(isset($_GET['choose_order']) and $_GET['choose_order']){
            $this->db->order_by('a.status desc,a.countdown asc');
        }
        $this->db->order_by('a.created','desc');
        $this->db->limit($limit,$limit*($page-1));
        $vdata['list']=$this->db->get()->result_array();
        $this->db->select('*');
        $this->db->from('boc_needs as a');
        $this->db->join('boc_account as b','a.uid=b.id','left');
        $this->db->join('boc_category as c','a.cate_id=c.id','left');
        $arr = array();
        if($this->cid==13){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['intro']) and $_GET['intro']){
                $arr['like']=array('a.intro',$_GET['intro']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            if(isset($_GET['claim_status']) and $_GET['claim_status'] >= 0){
                $arr['claim_status']=$_GET['claim_status'];
            }
            if(isset($_GET['arbitrate']) and $_GET['arbitrate']){
                $arr['arbitrate']=$_GET['arbitrate'];
            }
            $where1 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>1,'a.is_deleted'=>0);
            $where = array_merge($arr,$where1);
        }elseif($this->cid==14){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            $where2 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>2,'a.is_deleted'=>0);
            $where = array_merge($arr,$where2);
        }elseif($this->cid==15){
            if(isset($_GET['uname']) and $_GET['uname']){
                $arr['like']=array('b.uname',$_GET['uname']);
            }
            if(isset($_GET['cate_id']) and $_GET['cate_id']){
                $arr['cate_id']=$_GET['cate_id'];
            }
            $where3 = array('a.cid'=>13,"a.ccid"=>$this->ccid,'a.type'=>3,'a.is_deleted'=>0);
            $where = array_merge($arr,$where3);
        }
        $this->db->where($where);
        $all = $this->db->get()->result_array();
        $count = count($all);
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
        $total = $this->model->get_status_nums();
        $total2 = $this->model->get_status_nums2();
        $vdata['total'] = $total['total'];
        $vdata['total2'] = $total2['total'];
        $this->_display($vdata);
    }

    protected function _pages($url,$per_page=-1,$count=0,$uri=3){
        if (!$per_page < 0) {
            $per_page = $this->page_limit;
        }
        $this->load->library('pagination');
        $this->pagination->initialize(page_config($per_page,$count,$url,$uri));
        return $this->pagination->create_links();
    }

    public function delete($key = FALSE){
        if (!$key AND $this->input->get('ids') ) {
            $key = explode(',',$this->input->get('ids'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
        }
        if (!isset($vdata['status'])) {
            foreach($key as $vv){
                //悬赏任务及认领记录删除。如悬赏未结束，则退回押金至发布方余额
                //并交易记录中显示退回记录；如悬赏已结束，则资金不变动
                $need_info = $this->mneeds->get_one(array('id'=>$vv));
                if($need_info['type']==1){
                    $this->mneeds->update(array('is_deleted'=>1,'updated'=>time()),array('id'=>$vv));
                    $this->mclaim->update(array('is_deleted'=>1,'update_time'=>time()),array('need_id'=>$vv));
                    if($need_info['claim_status']==1 || $need_info['claim_status']==2 || $need_info['claim_status']==3 || $need_info['claim_status']==4){
                        //处理退回到发布方余额中
                        //处理悬赏押金退回
                        //查询补差价订单金额
                        $need_order_info = $this->mneedorder->get_one(array('need_id'=>$vv));
                        $need_order_money = 0;
                        if(!empty($need_order_info)){
                            $need_order_money = $need_order_info['money'];
                        }
                        $all_money = $need_order_money + $need_info['margin'];    //支出的总押金
                        $user_id = $need_info['uid'];
                        $this->db->query("update boc_account set balance=balance+$all_money where id=$user_id");
                        $this->mbalance->update(array('status'=>5,'updated_at'=>time()),array('need_id'=>$need_info['id']));   //任务关闭已退回
                        $balance_data_in2 = array(
                            'uid'=>$user_id,
                            'type'=>6,              //悬赏全额退回
                            'title'=>$need_info['intro'],
                            'need_id'=>$need_info['id'],
                            'money'=>$all_money,
                            'status'=>6,            //任务关闭退回
                            'pay_ways'=>3,          //余额收入
                            'pay_time'=>time(),
                            'created_at'=>time(),
                            'updated_at'=>time(),
                            'rid'=>$user_id,
                            'payment_sn'=>$need_info['payment_sn'],
                            'pay_code'=>$need_info['order_sn'],
                        );
                        $this->mbalance->create($balance_data_in2);
                    }
                }elseif($need_info['type']==2){
                    $this->mneeds->update(array('is_deleted'=>1,'updated'=>time()),array('id'=>$vv));
                    $this->mclaim->update(array('is_deleted'=>1,'update_time'=>time()),array('need_id'=>$vv));
                }else{
                    $this->mneeds->update(array('is_deleted'=>1,'updated'=>time()),array('id'=>$vv));
                }
            }
            $vdata = array('status'=>1,'msg'=>lang('modules_del_ok'));
            // 日志记录
            if (is_array($key)) {
                $this->mlogs->add('delete',lang('modules_del_id').$this->input->get('ids'));
            }else{
                $this->mlogs->add('delete',lang('modules_del_id').$key);
            }
        }
        if($this->input->is_ajax_request()){
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else{
            $this->load->view('msg',$vdata);
        }
    }
}
